
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.swing.text.html.HTML.Tag;

import controllers.Topics;

import models.*;
import play.db.jpa.Model;
import play.jobs.Job;
import play.jobs.OnApplicationStart;

@OnApplicationStart
public class Bootstrap extends Job {
			

 
	/**
	 * 
	 * this job initialize the database based on the yml file
	 * this serves the process of testing and was created to test search as well as other stories
	 * 
	 * @author Amr Draz
	 * @version 2011.0503
	 * @category C4S2
	 * @parameter none
	 * @return none
	 */
	
    public void doJob() {
        // Check if the database is empty
        if(User.count() == 0) {
        
  /// initializing users
			
    LinkedList<String> userAttributes=new LinkedList<String>();
    userAttributes.add("Amr");
    userAttributes.add("Draz");
    userAttributes.add("theDraz");
    userAttributes.add("thepass");
    userAttributes.add("0101132292");
    userAttributes.add("1 Hassan Aflaton, Heliopolis");
    userAttributes.add("amr.m.draz@gmail.com");
    
    User draz = new User(userAttributes);
    draz.setAdmin(true);
    draz.save();
    
    userAttributes = new LinkedList<String>();
    userAttributes.add("Nadder");
    userAttributes.add("Alexan");
    userAttributes.add("nader");
    userAttributes.add("pass");
    userAttributes.add("0101000000");
    userAttributes.add("somewhere in Egypt");
    userAttributes.add("alexan.nader@gmail.com");
   	User userNader = new User(userAttributes); // topic owner 	// sender
   	userNader.save();
   	
   	userAttributes = new LinkedList<String>();
    userAttributes.add("Joe");
    userAttributes.add("Selim");
    userAttributes.add("joe");
    userAttributes.add("pass");
    userAttributes.add("0101000000");
    userAttributes.add("somewhere in Egypt");
    userAttributes.add("youssef.a.selim@gmail.com");
	User userJoe = new User(userAttributes); // member 		// reciever
	userJoe.save();
	
	userAttributes = new LinkedList<String>();
	userAttributes.add("Amr");
    userAttributes.add("Draz");
    userAttributes.add("draz");
    userAttributes.add("pass");
    userAttributes.add("0101132292");
    userAttributes.add("Hassan Aflaton, Heliopolis");
    userAttributes.add("bing91@gmail.com");
	User userDraz = new User(userAttributes); // contributor
	userDraz.save();
	
	userAttributes = new LinkedList<String>();
	userAttributes.add("Ibrahim");
    userAttributes.add("Khaled");
    userAttributes.add("ibrahim");
    userAttributes.add("pass");
    userAttributes.add("0100100000");
    userAttributes.add("somewhere in Egypt");
    userAttributes.add("khaled.ibrahim@gmail.com");
	User userIbrahim = new User(userAttributes); // follower
	userIbrahim.save();
	
	userAttributes = new LinkedList<String>();
	userAttributes.add("Tarek");
    userAttributes.add("Sheasha");
    userAttributes.add("tarek");
    userAttributes.add("pass");
    userAttributes.add("0100100000");
    userAttributes.add("somewhere in Egypt");
    userAttributes.add("tarek.sheasha@gmail.com");
	User userTarek = new User(userAttributes); // organizer
	userTarek.save();
	
    userAttributes=new LinkedList<String>();
    userAttributes.add("Hossam");
    userAttributes.add("Saraya");
    userAttributes.add("admin");
    userAttributes.add("root");
    userAttributes.add("0100629998");
    userAttributes.add("Cairo");
    userAttributes.add("hossam.saraya@gmail.com");
        
    User hossam = new User(userAttributes);
    hossam.setAdmin(true);
    hossam.save();
    
/// initializing Tags
    
    models.Tag tag = new models.Tag();
    tag.save();
	tag.setName("Awesome");
	tag.setOwner(userNader);
	tag.addFollowers(userNader);
	tag.setStatus("Active");
//	tag.getTagNames().add("Nader");
//	tag.getTagNames().add("Alexan");
//	tag.getTagNames().add("Tag");
    tag.save();
    userNader.save();
    
/// this is where we initialize Organizations

    Organization guc = new Organization("GUC", userNader, "GUC description", "Public");
    guc.save();
	guc.addMember(userJoe); 		userJoe.save();
	guc.addMember(userDraz); 		userDraz.save();
	guc.addMember(userIbrahim); 	userIbrahim.save();
	guc.save();
    
    String name 		=           "Organization Null";
    String description 	=    		"An Organization so secret, it doesn't even exist";
    String privacy 		=        	"Secret";
    Organization orgNull = new Organization(name, draz, description, privacy);
    orgNull.save();
    
    name 				=           "PrivaOrg";
    description 		=    		"Hey, it's private";
    privacy 			=       	"Privat";
    Organization privaOrg = new Organization(name, draz, description, privacy);
    privaOrg.save();
    
    name 				=           "Organization NotNull";
    description 		=    		"An Organization that exists, this says nothing about " +
    								"a non-existent organization that no one knows about.";
    privacy 			=       	 "Public";
    Organization orgNotNull = new Organization(name, draz, description, privacy);
    orgNotNull.save();
    
/// this is where we initialize Entities
    
    name 				=           "Pootie Tang";
    description 		=    		"...";
    Eentity entityNull = new Eentity(draz, name, description);
    entityNull.setOrganization(orgNull);
    entityNull.save();
    
    name 				=           "Stuff";
    description 		=   		"stuff stuff stuff and more stuff";
    Eentity entityNotNull = new Eentity(draz, name, description);
    entityNotNull.setOrganization(orgNotNull);
    entityNotNull.save();
    
    name 				=           "privaEntity";
    description 		=    		"is even more private";
    Eentity privaEntity = new Eentity(draz, name, description);
    privaEntity.setOrganization(privaOrg);
    privaOrg.save();
    privaEntity.save();
    
    Eentity entity = new Eentity();
	entity.save();
	
    entity.setOrganization(guc);			guc.save();
	entity.addTag(tag); 					tag.save();
	entity.setOwner(userNader);				userNader.save();
	entity.addFollower(userIbrahim);		userIbrahim.save();
	entity.addMember(userJoe);				userJoe.save();
	entity.addOrganizer(userTarek);			userTarek.save();
	entity.save();
	
    Eentity e1 = new Eentity("Entity1", "Description1");
    e1.setOrganization(orgNotNull);
    e1.save();
    
    Role role1 = new Role(e1,"Organizer").save();
    hossam.getRoleList().add(role1);
    e1.save();
    hossam.getEntityList().add(e1);
    
    hossam.save();
    e1.getAllUsers().add(hossam);
    e1.save();

/// this initializes Topics
    
    String title 		= "AlZahimer is a big deal any thoughts";
    String content 		= "AlZahimer is a big deal, this topic will talk about it, any thoughts ideas are wilcome";
    String type			= "Public";
    Topic topic = new Topic(draz, title, content, type);
    topic.save();
    
    topic.setInitiator(draz);
    draz.save();

    topic.setInitiator(draz);
    draz.save();
    
    orgNotNull.getTopics().add(topic);
    orgNotNull.save();
    
    topic.setOrganization(orgNotNull);
    topic.setEntity(entityNotNull);
    topic.save();
    
    draz.getTopicList().add(topic);
    draz.save();
    
    /// another topic
    
    title 			 = "Nader's topic";
    content 		 = "Nader's content";
    type			 = "Public";
    Topic naderTopic = new Topic(userNader, title, content, type);
    naderTopic.save();
    
    naderTopic.addMember(userJoe);
    naderTopic.addContributor(userDraz);
    naderTopic.addFollowers(userIbrahim);
    naderTopic.setPromoted(true);
    naderTopic.setSpam(false);
    naderTopic.setEntity(entity);
    
    userNader.save(); userDraz.save(); userIbrahim.save(); userJoe.save();
    naderTopic.save();
    
    title 			 = "this topic should be private";
    content 		 = "this topic is private and you can't see it, na na na na na(unless you're a member)";
    type			 = "Private";
    Topic privaTopic = new Topic(draz, title, content, type);
    privaTopic.save();
    
    privaTopic.setInitiator(draz);
    draz.save();
    
    privaTopic.setEntity(privaEntity);
    privaOrg.save();
    privaTopic.save();
    
    draz.getTopicList().add(privaTopic);
    draz.save();
    
    
/// this is where we initialize Ideas
    
    LinkedList<User> creators = new LinkedList();
    creators.add(draz);
    title 				=           "Oblivion";
    String ideaDetail 	=    		"I can't remember";
    Idea secretIdea = new Idea(title, ideaDetail, false, entityNull, creators);
    secretIdea.setOrganization(orgNull);
    draz.save();
    secretIdea.save();
    
    creators = new LinkedList();
    creators.add(draz);
    title 				=           "Public Oblivion";
    ideaDetail 			=    		"I publicly can't remember";
    Idea publicIdea = new Idea(title, ideaDetail, false, topic, creators);
    publicIdea.setOrganization(orgNotNull);
    publicIdea.setEntity(entityNotNull);
    draz.save();
    publicIdea.save();
    
    creators = new LinkedList();
    creators.add(draz);
    title 				=           "Oblivion making up stuff";
    ideaDetail 			=    		"with enough imagination you'll never run out of shit to say, so let's make shit up";
    Idea publicIdea2 = new Idea(title, ideaDetail, false, entityNotNull, creators);
    publicIdea2.setOrganization(orgNotNull);
    draz.save();
    publicIdea2.save();
    
    creators = new LinkedList();
    creators.add(draz);
    title 				=           "Oblivion making up stuff";
    ideaDetail 			=    		"If I'm hidden then no one should see me, mwahahaha";
    Idea hiddenIdea = new Idea(title, ideaDetail, true, entityNotNull, creators);
    hiddenIdea.setOrganization(orgNotNull);
    draz.save();
    hiddenIdea.save();
    
    creators = new LinkedList();
    creators.add(draz);
    title 				=           "A Private Idea";
    ideaDetail 			=    		"also private so you can't see me, na na na na na(well maybe this discription)";
    Idea privaIdea = new Idea(title, ideaDetail, false, entityNotNull, creators);
    privaIdea.setOrganization(privaOrg);
    draz.save();
    privaIdea.save();
    
    creators = new LinkedList<User>();
	creators.add(userNader);
	title 				=           "Nader's Idea";
    ideaDetail 			=    		"Nader's idea details";  
    Idea idea = new Idea(title, ideaDetail, false, naderTopic, creators);
	//idea.getCreators().add(userNader);
	idea.save();
	
	idea.setDraft(false);
	idea.setActivityStatus(true);
	idea.setLock(false);
	idea.setSpam(false);
	idea.addTag(tag);
	idea.addMember(userJoe);

	
	Idea idea2 = new Idea("Nader's Idea", "Nader's idea details", false, topic, creators);
	idea2.setActivityStatus(true);
	idea2.setHide(false);
	idea2.setLock(false);
	idea2.setSpam(false);
	idea2.getTags().add(tag);
	idea2.getMembers().add(userJoe);
    
 ///Initialize plans
    
    Plan p1 = new Plan("Plan Awesome Pu");
  //  p1.save();
    p1.getIdeas().add(publicIdea);
    p1.save();
    
    Plan p2 = new Plan("Plan Awesome Pr");
    p2.getIdeas().add(privaIdea);
    p2.save();
    

	LinkedList<String> requirements = new LinkedList<String>();
	requirements.add("requirements 1");
	requirements.add("requirements 2");
	requirements.add("requirements 3");
	
	
    Plan plan = new Plan("Nader's Plan");
    plan.save();
    
	plan.addUser(userNader); 	userNader.save();
	plan.addUser(userJoe); 		userJoe.save();
	
	Item item = new Item("Item's Name", "Item's content", new Date(), plan);
	item.setCompleted(false);
	item.save();
	item.addUser(userNader);
	item.addTag(tag);
    item.save();
    
/// initializing rating
    
	Rating ratingIdea = new Rating();
	ratingIdea.setRater(userNader);
	ratingIdea.setRating("high");
	ratingIdea.setIdea(idea2);
	ratingIdea.save();

    
/// Initialize comments
    
    content = "Pootie needs no words";
    boolean spam = false;
    Comment comment = new Comment(draz, content, type, spam, topic.id);
    comment.save();

    Comment commentIdea;// = new Comment(userNader, "Nader's comment on the idea", "Idea", false, idea.getId());
	commentIdea = new Comment();
	commentIdea.setSender(userNader);
	commentIdea.setContent("Nader's comment on the idea");
	commentIdea.setSpam(false);
	commentIdea.setIdea(idea);
	commentIdea.save();
	userNader.addComment(commentIdea);
	userNader.save();
	
	Comment commentIdea2; // = new Comment(userNader, "Nader's comment on the idea2", "Idea", false, idea2.getId());
	commentIdea2 = new Comment();
	commentIdea2.setSender(userNader);
	commentIdea2.setContent("Nader's comment on the idea2");
	commentIdea2.setSpam(false);
	commentIdea2.setIdea(idea2);
	commentIdea2.save();
	userNader.addComment(commentIdea2);
	userNader.save();
	
	Comment commentPlan; // = new Comment(userJoe, "Joe's comment on the plan", "Plan", false, plan.getId());
	commentPlan = new Comment();
	commentPlan.setSender(userJoe);
	commentPlan.setContent("Joe's comment on the plan");
	commentPlan.setSpam(false);
	commentPlan.setPlan(plan);
	commentPlan.save();
	userJoe.addComment(commentPlan);
	userJoe.save();
	
	Comment commentTopic;// = new Comment(userDraz, "Draz's comment on the topic", "Topic", false, topic.getId());
	commentTopic = new Comment();
	commentTopic.setSender(userDraz);
	commentTopic.setContent("Draz's comment on the topic");
	commentTopic.setSpam(false);
	commentTopic.setTopic(topic);
	commentTopic.save();
	userDraz.addComment(commentTopic);
    userDraz.save();   
    
/// Initialize notifications
    
    Notification notification = new Notification(userIbrahim, userDraz, "Noficiation subject", "The conent of the notification", "12.2.2001");
    notification.save();
    
/// initialize requests
    
    Request request = new Request(userNader, userJoe, "Request content", false, "Public");

	Comment commentRequest;// = new Comment(userIbrahim, "Ibrahim's comment on the request", "Request", false, request.getId());
	commentRequest = new Comment();
	commentRequest.setSender(userIbrahim);
	commentRequest.setContent("Ibrahim's comment on the request");
	request.save();
	commentRequest.setRequest(request);
	commentRequest.save();
	userIbrahim.addComment(commentRequest);
	
	request.getComments().add(commentRequest);
	
/// initializing invitation
	

	Invitation invitation = new Invitation(userNader, userJoe, "invitation sent from Nader to Joe about organization", entity);
	invitation.setStatus("Pending");
	invitation.save();
	
/////////////////////////////////// late addition
	
	topic.addIdea(idea);
	topic.addIdea(idea2);
	topic.addComment(commentTopic);
	topic.getItems().add(item);
	topic.addTag(tag);
	topic.setEntity(entity);
	topic.setOrganization(guc);
	topic.setEntity(entity);
	
	
	tag.addOrganization(guc);
	tag.addIdea(idea);
	tag.addIdea(idea2);
	tag.addEntity(entity);
	tag.addItem(item);
	
	idea.setOrganization(guc);
	idea.setEntity(entity);
	idea.setTopic(topic);
	idea.addComment(commentIdea);
	idea.setPlan(plan);
	idea.setViews(4);
	
	idea2.setOrganization(guc);
	idea2.setEntity(entity);
	idea2.setTopic(topic);
	idea2.addComment(commentIdea2);
	idea2.setPlan(plan);
	idea2.setViews(4);
	
	entity.setOrganization(guc);
	entity.addTag(tag);
//	entity.getRelatedItems().add(item);//no idea what this is for
	entity.getInvitations().add(invitation);
	entity.addTopic(topic);
	entity.addIdea(idea);
	entity.addIdea(idea2);
	entity.setOwner(userNader);
	entity.addFollower(userIbrahim);
	entity.addMember(userJoe);
	entity.addOrganizer(userTarek);
	entity.save();
	
	plan.getItems().add(item);
	plan.getComments().add(commentPlan);
    
/////////////////////////////////// saving again just in case
    draz.save();
    hossam.save();
    userNader.save();
	userJoe.save();
	userDraz.save();
	userIbrahim.save();
    
    orgNotNull.save();
    orgNull.save();
    privaOrg.save();
    
    entityNotNull.save();
    entityNull.save();
    privaEntity.save();
    e1.save();
    
    topic.save();
    
    hiddenIdea.save();
    publicIdea2.save();
    publicIdea.save();
    secretIdea.save();
    privaIdea.save();
    
    naderTopic.save();
	tag.save();
	idea.save();
	idea2.save();
	ratingIdea.save();
	entity.save();
	invitation.save();
	plan.save();
	item.save();
	request.save();
	notification.save();
	guc.save();
	commentIdea.save();
	commentIdea2.save();
	commentPlan.save();
	commentTopic.save();
	commentRequest.save();
    
        }
        
        org.hsqldb.util.DatabaseManagerSwing.main(new String[] { "--url", 
	    		   "jdbc:hsqldb:mem:playembed", "--noexit" }); 
    }
}
